package com.easy.frame.servers.sys.controller.staff;


import com.easy.frame.base.api.pojo.PageDTO;
import com.easy.frame.base.api.pojo.PageVO;
import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.dto.SysStaffDTO;
import com.easy.frame.servers.sys.entity.SysStaff;
import com.easy.frame.servers.sys.service.staff.SysStaffService;
import com.easy.frame.servers.sys.vo.SysStaffVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 员工表相关接口类
 *
 * @author tp
 * @date 2021/1/7 09:39
 */
@Slf4j
@Api(tags = "员工/sys-staff")
@RestController
@RequestMapping("/sys-staff")
public class SysStaffController extends BaseController {

    @Resource
    SysStaffService sysStaffService;

    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysStaffVO>> page(@RequestBody PageDTO<SysStaffDTO> param) {
        PageVO<SysStaffVO> result = sysStaffService.selectForPage(param);
        return success(result);
    }

    @ApiOperation("查询-根据id")
    @GetMapping("/by/id")
    public ServiceResponse<SysStaffVO> getById(@ApiParam(value = "主键id", required = true) @RequestParam Long id) {
        return success(sysStaffService.selectVoById(id));
    }

    @ApiOperation("保存")
    @PostMapping
    public ServiceResponse<?> saveStaff(@RequestBody SysStaffDTO entity) {
        sysStaffService.saveStaff(entity);
        return success();
    }

    @ApiOperation("修改-根据id")
    @PutMapping
    public ServiceResponse<SysStaffDTO> update(@RequestBody SysStaffDTO entity) {
        sysStaffService.updateStaff(entity);
        return success(entity);
    }

    @ApiOperation("根据username修改密码")
    @PutMapping("/by/name")
    public ServiceResponse<SysStaff> updatePassword(@RequestBody SysStaff entity) {
        sysStaffService.updateByName(entity);
        return success(entity);
    }

    @ApiOperation("删除by id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true) @RequestParam Long id) {
        SysStaff sysStaff = sysStaffService.selectById(id);
        if (StringUtils.isEmpty(sysStaff)) {
            return fail("删除失败，无效数据");
        }
        sysStaffService.updateDeleted(id);
        return success();
    }

}
